﻿/*
Problem 1 – 9Gag Numbers
In 9Gag we like fun. We have a lot of time and we play with fun pictures and fun stories. Recently we invented a funny way to express numbers. We use the following 9 digits:
0	-!
1	**
2	!!!
3	&&
4	&-
5	!-
6	*!!!
7	&*!
8	!!**!-
We write the numbers as sequences digits from our 9 available digits given above. The last digit of the number (the most right one) has a value as shown in the above table. The next digit on the left has a value 9 times bigger than the shown in the above table, the next digit on the left has 9*9 times bigger value than the shown in the table and so on. Your task is to write a program to convert a 9Gag-style number into its corresponding decimal representation.
Input
The input data consists of a single string – a 9Gag-style number.
The input data will always be valid and in the described format. There is no need to check it explicitly.
Output
The output data consists of a single line holding the calculated decimal representation of the given 9Gag-style number and should be printed at the console.
Constraints
•	The input number will have between 1 and 20 digits.
•	Allowed working time for your program: 0.1 seconds.
•	Allowed memory: 16 MB.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace Problem1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            BigInteger result = 0;
            int offset = 1;
            List<int> digits = new List<int>();
            int i = 0;
            int n = input.Length;
            while (i < n)
            {
                string sub2 = input.Substring(i, 2);
                if ((i < n - 5) && (input.Substring(i, 6) == "!!**!-")) { digits.Add(8); offset = 6; }
                else
                {
                    if ((i < n - 2) && (input.Substring(i, 3) == "&*!")) { digits.Add(7); offset = 3; }
                    else
                    {
                        if ((i < n - 3) && (input.Substring(i, 4) == "*!!!")) { digits.Add(6); offset = 4; }
                        else
                        {
                            if (sub2 == "!-") { digits.Add(5); offset = 2; }
                            else
                            {
                                if (sub2 == "&-") { digits.Add(4); offset = 2; }
                                else
                                {
                                    if (sub2 == "&&") { digits.Add(3); offset = 2; }
                                    else
                                    {
                                        if ((i < n - 2) && (input.Substring(i, 3) == "!!!")) { digits.Add(2); offset = 3; }
                                        else
                                        {
                                            if (sub2 == "**") { digits.Add(1); offset = 2; }
                                            else if (sub2 == "-!") { digits.Add(0); offset = 2; }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                i = i + offset;
            }
            int k = 0;
            for (int j = digits.Count - 1; j >= 0; j--)
            {
                result = result + digits[j] * Power(9, k);
                k++;
            }
            Console.WriteLine(result);
        }
        public static BigInteger Power(int a, int b)
        {
            BigInteger result = 1;
            for (int i = 0; i < b; i++)
            {
                result = result * 9;
            }
            if (b < 1) result = 1;
            return result;
        }
    }
}
